package com.view.jurisdictionmanage;

import com.bean.Power;
import com.bean.Role;
import com.bean.User;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoCheckBox;
import com.lovo.netCRM.component.LovoTxt;
import com.service.PowerService;
import com.service.RoleService;
import com.service.UserService;
import com.service.impl.PowerServiceImpl;
import com.service.impl.RoleServiceImpl;
import com.service.impl.UserServiceImpl;
import com.util.MyCheckBox;
import com.view.MainFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.util.List;

public class UserUpdateDialog extends JDialog {
    private LovoButton addBtn;
    private LovoButton cancelBtn;
    private JLabel nameTxt,realTxt;
    private MyCheckBox roleCbx;
    private Checkbox checkbox;
    private JLabel powerLeb;

    private int key;
    private MainFrame frame;
    private UserService userService = new UserServiceImpl();
    private RoleService roleService = new RoleServiceImpl();
    private PowerService powerService = new PowerServiceImpl();

    public UserUpdateDialog(MainFrame frame,int key) {
        // TODO Auto-generated constructor stub
        super(frame,"修改用户",true);
        this.frame = frame;
        this.key = key ;
        this.setBounds(350, 250, 520, 400);
        this.setLayout(null);
        cbxFlush();
        this.init();
        this.setVisible(true);
    }


    public void cbxFlush(){
        String[] strs = roleService.getAllRoles();
        roleCbx = new MyCheckBox("角色",strs,20,100,this){
            @Override
            public void addListener(){
                String[] strings = roleCbx.getItem();
                List<String> stringList = new ArrayList<>();
                stringList = Arrays.asList(strings.clone());
                Set<String> powers = new HashSet<>();
                for (String str:stringList) {
                    List<Power> powerList = new ArrayList<>();
                    powerList = powerService.getPower(str);
                    System.out.println(powerList);
                    for (Power power:powerList) {
                        String powerName = power.getPowerName();
                        powers.add(powerName);
                    }
                }
                powerLeb.setText("<html>拥有权限:"+powers+"</html>");
            }
        };

        List<User> userList = new ArrayList<>();
        List<String> roles = new ArrayList<>();
        userList = userService.getAllUsers();
        for (User user:userList) {
            int id = user.getId();
            if (id == key) {
                List<Role> roleList = new ArrayList<>();
                roleList = user.getRoleList();
                for (Role role:roleList) {
                    String roleName = role.getRoleName();
                    roles.add(roleName);
                }
            }
        }
        String[] strings = roles.toArray(new String[roles.size()]);
        roleCbx.setItem(strings);
    }

    private void init() {
        // TODO Auto-generated method stub
        User newUser = new User();
        newUser = userService.getSelectUser(key);

        nameTxt = new JLabel();
        nameTxt.setBounds(20,20,200,50);
        nameTxt.setText("用户名:    "+newUser.getAccount());
        this.add(nameTxt);


        realTxt = new JLabel();
        realTxt.setBounds(300,20,200,50);
        realTxt.setText("<html>真实姓名:    "+newUser.getRealName()+"</html>");
        this.add(realTxt);

        powerLeb = new JLabel();
        powerLeb.setBounds(50,200,350,150);
        powerLeb.setText("拥有权限:");
        this.add(powerLeb);

        this.addBtn = new LovoButton("添加", 120, 320, this);
        this.cancelBtn = new LovoButton("取消", 270, 320, this);

        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String[] strings = roleCbx.getItem();
                List<String> rolenames = Arrays.asList(strings);
                System.out.println(rolenames);
                if(userService.updateUserRole(key,rolenames)){
                    JOptionPane.showMessageDialog(null,"修改成功");
                    UserUpdateDialog.this.dispose();
                    frame.flush();
                }else {
                    JOptionPane.showMessageDialog(null,"修改失败");
                }
            }
        });

        cancelBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UserUpdateDialog.this.dispose();
            }
        });

    }

}
